/*
 * DailyRewardResource.cpp
 *
 *  Created on: 2013. 9. 6.
 *      Author: mega@gamevil.com
 */

#include "DailyRewardResource.h"
#include "../NUtil/Utils.h"
#include "../NUtil/Pool.h"
#include <Database/Static.h>

namespace ProjectZ { namespace NResource {


DailyRewardData::~DailyRewardData()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
}

void
DailyRewardData::Clear()
{
	std::for_each(clsFlyTree.begin(), clsFlyTree.end(), ProjectZ::NUtil::del_second_ptr<DATATREE::value_type>);
	clsFlyTree.clear();
}

bool DailyRewardData::LoadResource(int company, int saleCode)
{
	::Database::Command::Auto clsAuto(::Database::GETCOMMANDPOOL());
	clsAuto->SetQueryType(::Database::Command::SELECT);
	clsAuto->MakeQuery(" SELECT NATIONAL_CODE, flag, date_index, icon_index, reward_type, sheet_type, reward_tid, value FROM resource_daily_reward WHERE company=%d AND sale_cd=%d", company, saleCode);

	if (false == ::Database::GETSESSION()->SyncExecute(1, clsAuto))
		return false;

	if( clsAuto->GetErrorIdx() != 0 )
		return false;

	if (0 == clsAuto->ResultRowCount())
		return false;

	Clear();

	int oldFlag = 0, maxDateIndex = 0;
	while( clsAuto->Next() )
	{
		::Toolkit::Str<3> NATIONAL_CODE;
		DailyRewardData::Flyweight * pFly = new DailyRewardData::Flyweight;

		int flag = 0, dateIndex = 0;
		clsAuto->GetRecord() >> NATIONAL_CODE >> flag >> dateIndex >> pFly->_ICON_INDEX >> pFly->_REWARD_TYPE >> pFly->_SHEET_TYPE
				>> pFly->_REWARD_TID >> pFly->_VALUE;

		if (0 != Config::Instance().GetResourceDatabase()._strNational.compare(NATIONAL_CODE.c_str()))
		{
			DEBUGLOG("%s:%d NATIONAL_CODE ERROR!!! Config National Code [%s] , Resource National Code [%s]",
					__FILE__, __LINE__,
					Config::Instance().GetResourceDatabase()._strNational.c_str(),
					NATIONAL_CODE.c_str());
			return false;
		}

		clsFlyTree[DailyRewardData::Key(flag, dateIndex)] = pFly;

		if (maxFlag < flag)
			maxFlag = flag;

		if (maxDateIndex < dateIndex)
		{
			maxDateIndex = dateIndex;
			clsMaxKeyTree[flag] = dateIndex;
		}

		if (oldFlag != flag)
		{
			oldFlag = flag;
			maxDateIndex = 0;
		}
	}

	return true;
}

DailyRewardData::Flyweight * DailyRewardData::GetFly(DailyRewardData::Key & clsKey)
{
	auto iter = clsFlyTree.find(clsKey);

	if ( iter == clsFlyTree.end() )
		return NULL;
	else
		return iter->second;
}

int DailyRewardData::GetMaxDate(int flag)
{
	auto iter = clsMaxKeyTree.find(flag);

	if (iter == clsMaxKeyTree.end())
		return 0;
	else
		return iter->second;
}

DailyRewardResource::DailyRewardResource()
{
	pDailyRewardAndroid = new DailyRewardData;
	pDailyRewardIOS 	= new DailyRewardData;
}

DailyRewardResource::~DailyRewardResource()
{
	delete pDailyRewardAndroid;
	delete pDailyRewardIOS;
}

bool DailyRewardResource::LoadResource()
{
	if (false == pDailyRewardAndroid->LoadResource(MARKET_GOOGLE, SC_KAKAO_GOOGLE))
		return false;

	if (false == pDailyRewardIOS->LoadResource(MARKET_IOS, SC_KAKAO_IOS))
		return false;

	return true;
}

DailyRewardData::Flyweight * DailyRewardResource::GetFly(int company, int saleCode, DailyRewardData::Key & clsKey)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(company, saleCode);

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pDailyRewardAndroid->GetFly(clsKey);

	case NUtil::MarketType::IOS_KAKAO:
		return pDailyRewardIOS->GetFly(clsKey);

	default:
		return NULL;
	}

	return NULL;
}

int DailyRewardResource::GetMaxFlag(int company, int saleCode)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(company, saleCode);

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pDailyRewardAndroid->GetMaxFlag();

	case NUtil::MarketType::IOS_KAKAO:
		return pDailyRewardIOS->GetMaxFlag();

	default:
		return 0;
	}

	return 0;
}

int DailyRewardResource::GetMaxDate(int company, int saleCode, int flag)
{
	NUtil::MarketType marketType = NUtil::GetMarketType(company, saleCode);

	switch (marketType)
	{
	case NUtil::MarketType::AD_KAKAO:
		return pDailyRewardAndroid->GetMaxDate(flag);

	case NUtil::MarketType::IOS_KAKAO:
		return pDailyRewardIOS->GetMaxDate(flag);

	default:
		return 0;
	}

	return 0;
}

} /*NResource*/ } /* namespace ProjectZ */
